package com.baosight.risk.service.riim.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baosight.risk.api.fltd.FltdProcessService;
import com.baosight.risk.api.riim.RiimRiskImportConfirmService;
import com.baosight.risk.api.workflow.AfterHandler;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.mapper.fltd.FltdTaskMapper;
import com.baosight.risk.mapper.riim.RiimRiskImportMapper;
import com.baosight.risk.mapper.xtzy.XtzyUserMapper;
import com.baosight.risk.service.fltd.entity.FltdTask;
import com.baosight.risk.service.fltd.vo.FltdTaskVO;
import com.baosight.risk.service.riim.entity.RiimRiskImport;
import com.baosight.risk.service.xtzy.entity.XtzyUser;

import lombok.extern.slf4j.Slf4j;
@Service("riimRiskImportConfirmServiceImpl")
@Slf4j
public class RiimRiskImportConfirmServiceImpl extends BaseServiceImpl<RiimRiskImport> implements RiimRiskImportConfirmService, AfterHandler {
    @Autowired
    private RiimRiskImportMapper riskImportMapper;
    @Autowired
    private FltdTaskMapper fltdTaskMapper;
    @Autowired
    private XtzyUserMapper xtzyUserMapper;
    @Autowired
    private FltdProcessService fltdProcessService;
    
	@Override
	public BaseMapper getMapper() {
		// TODO Auto-generated method stub
		return riskImportMapper;
	}

	/**
	 * 上级管理单位-汇总审核
	 */
	@Override
	public boolean doAfter(Map<String, Object> params){
		String id = (String)params.get("bizGuid");
		RiimRiskImport riskImport = riskImportMapper.selectByPrimaryKey(id);
		RiimRiskImport param = new RiimRiskImport();
		param.setParentId(riskImport.getId());
		List<RiimRiskImport> subPlans = riskImportMapper.select(param);
		
		String submitType = (String)params.get("submitType");
		FltdTaskVO paramTask=new FltdTaskVO();
		paramTask.setFlowNo("jidishishifangan");
        paramTask.setNodeCode("C01");
        paramTask.setStatus("open");
		for(RiimRiskImport subPlan : subPlans){
	        paramTask.setBizGuid(subPlan.getId());
	        List<FltdTask> taskList = fltdTaskMapper.selectTaskList(paramTask);
	        
            if(taskList.size()>0){
            	XtzyUser xtzyUser = xtzyUserMapper.selectByName(subPlan.getDutyUserCode());
                FltdTaskVO taskVoParam=new FltdTaskVO();
                BeanUtils.copyBeanProp(taskVoParam, taskList.get(0));
                try {
                	if("submit".equals(submitType)){
                		fltdProcessService.doSubmit(xtzyUser,taskVoParam);
                	}else if("reject".equals(submitType)){
                		fltdProcessService.doCancel(xtzyUser,taskVoParam);
                	}
				} catch (Exception e) {
					throw new RuntimeException(e.getMessage());
				}
            }
		}
		return true;
	}
	
}
